<template class="login_page">
  <div class="login_page fillcontain">
    <nav-menu :pageIndex="pageIndex"></nav-menu>
    <transition name="form-fade" mode="in-out">
      <section class="form_contianer">
        <div class="manage_tip">
          <p>用户注册</p>
        </div>
        <el-form :model="registerForm" :rules="rules" ref="registerForm">
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" placeholder="用户名"><span>dsfsf</span></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" placeholder="密码" v-model="registerForm.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPwd">
            <el-input type="password" placeholder="确认密码" v-model="registerForm.confirmPwd"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('registerForm')" class="submit_btn">注册</el-button>
          </el-form-item>
        </el-form>
        <a href="./#/login" class="tip">已有账号？点此登录</a>
      </section>
    </transition>
  </div>
</template>

<script>
import NavMenu from "@/components/NavMenu.vue";
import Vue from "vue"; //导航栏组件
export default {
  name: "Register",
  components: {
//    SearchLink,
    NavMenu,
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.registerForm.confirmPwd !== '') {
          this.$refs.registerForm.validateField('confirmPwd');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      pageIndex: "1", //当前页的序号
      registerForm: {
        username: '',
        password: '',
        confirmPwd: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur'},
          {validator: validatePass, trigger: 'blur'}
        ],
        confirmPwd: [
          {required: true, message: '请再次确认密码', trigger: 'blur'},
          {min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur'},
          {validator: validatePass2, trigger: 'blur', required: true}
        ]
      }
    }
  },
  mounted() {
    // document.body.style.backgroundColor="#324057";
    this.init()
  },
  methods: {
    init() {
      let isLogin = false
      if (localStorage.getItem('userLoginStatus') === 'true') {
        isLogin = true
      } else {
        isLogin = false
      }
      if (isLogin) {
        this.$router.push('/')
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          Vue.prototype.post('http://192.168.0.94:2226/register', {
            id: this.registerForm.username,
            pwd: this.registerForm.password
          }, this.registerCheck)
        } else {
          this.$notify.error({
            title: '错误',
            message: '请输入正确的用户名密码',
            offset: 100
          });
          return false;
        }
      });
    },
    registerCheck(res) {
      // 向后台验证账号是否可以注册，返回status，1代表可以注册，0代表不能
      // res.message 写明原因，如用户名已存在，或者其他原因

      // 注册成功自动跳转至登录界面
      if (res.data.retcode === 0) {
        this.$message({
          type: 'success',
          message: '注册成功,自动跳转至登录界面'
        });
        this.$router.push('/login')
      } else {
        this.$message({
          type: 'error',
          message: '注册失败，服务器拒绝本次注册'
          // message: res.message
        });
      }
    }
  }
}
</script>

<style lang="less" scoped>
@import '../style/mixin';

.login_page {
  background-color: #324057;
  height: 100%;
  width: 100%;
}

.manage_tip {
  position: absolute;
  width: 100%;
  top: -100px;
  left: 0;

  p {
    font-size: 34px;
    color: black;
  }
}

.form_contianer {
  border: 1px solid #ededed;
  .wh(320px, 210px);
  .ctp(320px, 210px);
  padding: 25px;
  border-radius: 5px;
  text-align: center;
  background-color: #fff;

  .submit_btn {
    width: 100%;
    font-size: 16px;
  }
}

.tip {
  font-size: 18px;
  color: blue;
  float: right;
  text-decoration-line: none
}

.form-fade-enter-active, .form-fade-leave-active {
  transition: all 1s;
}

.form-fade-enter, .form-fade-leave-active {
  transform: translate3d(0, -50px, 0);
  opacity: 0;
}
</style>

